import axios from 'axios'
import store from "@/store"
import { Message } from 'element-ui'
import message from "element-ui/packages/message";

// 创建一个新的axios实例
const service = axios.create({
  // baseURL: 'http://localhost:6663/api', // 本地开发地址
  baseURL: 'http://120.46.160.17:6663/api', // 远程服务器地址
  timeout: 10000,
  withCredentials:true
})

// 配置请求拦截器
service.interceptors.request.use(config => {
  // 判断是否带有token
  if (store.getters.token) {
    config.headers.Authorization = `${store.getters.token}`
  }
  return config
}, error => {
  return Promise.reject(error)
})

// 配置响应拦截器
service.interceptors.response.use(response => {
  const { data, msg, code } = response.data
  if (code === 200) {
    return data
  } else {
    Message({
      type: 'error',
      message: msg
    })
    return Promise.reject(new Error(msg))
  }
}, async (error) => {
  Message({
    type: 'error',
    message: error.message
  })
  return Promise.reject(error)
})

export default service
